package com.example.job.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.job.R;

import com.example.job.db.jobdb;
import com.example.job.db.userdb;
import com.example.job.entity.Job;
import com.example.job.entity.UserInfo;

import java.io.InputStream;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText lg_username;
    private EditText lg_password;
    private CheckBox checkbox;
    private boolean is_login;

    private SharedPreferences lgSharedPreferences;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        lg_username = findViewById(R.id.lg_user);
        lg_password = findViewById(R.id.lg_password);
        checkbox = findViewById(R.id.checkbox);


        lgSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        //判断是否记住密码
        is_login = lgSharedPreferences.getBoolean("is_login",false);
        if(is_login){
            String username = lgSharedPreferences.getString("username",null);
            String password = lgSharedPreferences.getString("password",null);
            lg_username.setText(username);
            lg_password.setText(password);
            checkbox.setChecked(true);
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.go_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = lg_username.getText().toString();
                String password = lg_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show();
                }else{
                    UserInfo login = userdb.getInstance(LoginActivity.this).login(username);
                    if(login != null){
                        if(username.equals(login.getUsername()) && password.equals(login.getPassword())){
                            SharedPreferences.Editor editor = lgSharedPreferences.edit();
                            editor.putBoolean("is_login",is_login);
                            editor.putString("username",username);
                            editor.putString("password",password);
                            editor.commit();
                            //登录成功
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);


                        }else {
                            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(LoginActivity.this,"该账号暂未注册",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                is_login = b;
            }
        });

        // 添加用户删除表按钮的点击事件
//        Button dropTableButton = findViewById(R.id.drop_table_button);
//        dropTableButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                userdb.getInstance(LoginActivity.this).dropAndRecreateUserTable();
//                Toast.makeText(LoginActivity.this, "表已删除并重新创建", Toast.LENGTH_SHORT).show();
//            }
//        });

    }
}